package com.lanou.moving.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.SearchSingerRoot;
import com.lanou.moving.entity.SingerDetail;
import com.lanou.moving.entity.SingerRoot;
import com.lanou.moving.entity.dao.Collection;
import com.lanou.moving.entity.dao.CollectionDao;
import com.lanou.moving.fragment.SingerAlbumFragment;
import com.lanou.moving.fragment.SingerInfoFragment;
import com.lanou.moving.fragment.SingerMvFragment;
import com.lanou.moving.fragment.SingerSongFragment;
import com.lanou.moving.singleton.DaoSingleton;
import com.lanou.moving.view.CDSPopupWindow;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/23.
 */
public class SingerDetailActivity extends BaseActivity
        implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.singer_collapsing_toolbar)
    CollapsingToolbarLayout singerCollapsingToolbar;
    @Bind(R.id.singer_songs)
    RadioButton singerSongs;
    @Bind(R.id.singer_albums)
    RadioButton singerAlbums;
    @Bind(R.id.singer_mvs)
    RadioButton singerMvs;
    @Bind(R.id.singer_info)
    RadioButton singerInfo;
    @Bind(R.id.singer_detail_rg)
    RadioGroup singerDetailRg;
    @Bind(R.id.singer_detail)
    FrameLayout singerDetail;
    @Bind(R.id.singer_detail_picture)
    ImageView singerDetailPicture;

    private SingerRoot.DataEntity dataEntity;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private int id;
    private int singer_id;
    private SingerAlbumFragment albumFragment;// 专辑
    private SingerInfoFragment infoFragment;// 歌手信息
    private SingerMvFragment mvFragment;// MV
    private SingerSongFragment songFragment;// 单曲
    private VolleySingleton volleySingleton;
    private StringRequestUtf request;
    private SingerDetail.DataEntity data;
    private SearchSingerRoot.DataEntity entity;
    private Toolbar singer_toolbar;// 返回

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_detail);
        BaseApplication.getInstance().addActivity(this);
        ButterKnife.bind(this);

        singer_toolbar = (Toolbar) findViewById(R.id.singer_toolbar);
        singer_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        volleySingleton = VolleySingleton.getInstance(this);
        Intent intent = getIntent();
        singer_id = intent.getIntExtra("singer_id", 0);

        getData();

        singerDetailRg.setOnCheckedChangeListener(this);
        albumFragment = new SingerAlbumFragment();
        infoFragment = new SingerInfoFragment();
        mvFragment = new SingerMvFragment();
        songFragment = new SingerSongFragment();

        // 给Fragment传值
        Bundle bundle = new Bundle();
        bundle.putInt("singer_id", singer_id);
        albumFragment.setArguments(bundle);
        infoFragment.setArguments(bundle);
        mvFragment.setArguments(bundle);
        songFragment.setArguments(bundle);

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.singer_detail, songFragment);

        transaction.commit();
    }


    public void getData() {
        String url = URL.SINGER_START + singer_id + URL.SINGER_END;
        request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SingerDetail singerDetail = gson.fromJson(response, SingerDetail.class);
                data = singerDetail.getData();
                Picasso.with(SingerDetailActivity.this).load(data.getPicUrl()).placeholder(R.mipmap.singer_detail)
                        .error(R.mipmap.singer_detail).into(singerDetailPicture);
                singerCollapsingToolbar.setTitle(data.getName());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.singer_songs:
                transaction = manager.beginTransaction();
                transaction.replace(R.id.singer_detail, songFragment);
                transaction.commit();
                break;
            case R.id.singer_albums:
                transaction = manager.beginTransaction();
                transaction.replace(R.id.singer_detail, albumFragment);
                transaction.commit();
                break;
            case R.id.singer_mvs:
                transaction = manager.beginTransaction();
                transaction.replace(R.id.singer_detail, mvFragment);
                transaction.commit();
                break;
            case R.id.singer_info:
                transaction = manager.beginTransaction();
                transaction.replace(R.id.singer_detail, infoFragment);
                transaction.commit();
                break;
        }
    }


}
